package com.gamerole.app.ui

import android.content.Intent
import android.net.Uri
import android.provider.Browser
import android.view.View
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.paging.LoadState
import com.gamerole.app.R
import com.gamerole.app.databinding.AppFragmentArticleBinding
import com.gamerole.app.databinding.AppFragmentUpSearchBinding
import com.gamerole.app.databinding.AppRecyItemChatBinding
import com.gamerole.app.entity.ArticleBean
import com.gamerole.app.entity.bus.PanSearchEvent
import com.gamerole.app.page.adapter.ArticleAdapter
import com.gamerole.app.page.adapter.UpSearchAdapter
import com.gamerole.app.viewmodel.ChatViewModel
import com.gamerole.app.viewmodel.UpSearchViewModel
import com.gamerole.commom.adapter.ItemClickSupport
import com.gamerole.commom.adapter.slimadapter.SlimAdapter
import com.gamerole.commom.base.BaseFragment
import com.gamerole.commom.extention.image
import com.gamerole.commom.mvchelper.PageDataSource
import com.hi.dhl.binding.viewbind
import com.jansir.eventbus.core.observeEvent
import com.shizhefei.mvc.MVCSwipeRefreshHelper
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch


@AndroidEntryPoint
class UpSearchFragment() :
    BaseFragment(R.layout.app_fragment_up_search) {
    private val viewModelChat: UpSearchViewModel by viewModels()
    override fun getViewModel() = viewModelChat
    private val binding: AppFragmentUpSearchBinding by viewbind()
    private lateinit var articleAdapter: UpSearchAdapter

    override fun lazyInitData() {
    }

    override fun initView() {
        articleAdapter = UpSearchAdapter()
        binding.recyclerViewNested.adapter = articleAdapter

        binding.refreshLayout.setOnRefreshListener {
            articleAdapter.refresh()
        }
        articleAdapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    // 根据LoadState，结束smartRefreshLayout的对应状态
                    binding.refreshLayout.isRefreshing = false
                }
                is LoadState.Loading -> {
                }
                is LoadState.Error -> {
                }
            }
        }

        ItemClickSupport.addTo(binding.recyclerViewNested)
            .setOnItemClickListener { recyclerView, position, v ->
                run {
                    val articleBean = articleAdapter.peek(position)!!
                    val intent = Intent(
                        Intent.ACTION_VIEW
                    )
                    intent.addCategory(Intent.CATEGORY_DEFAULT);
                    intent.data = Uri.parse("smartdrive://share/browse?sid=lnsx388gu9hcr&shareId=${articleBean.page_url.split("/")[4]}&sharePwd=&click_position=page_sharing_top&source_address=Page_sharing_file&source_type=scheme");
                    requireActivity().startActivity(intent)
                }
            }

    }

    override fun initEvent() {
        observeEvent(PanSearchEvent::class.java, true) {
            var items = viewModelChat.getPageData(it.keyword)
            lifecycleScope.launchWhenCreated {
                items.collectLatest {
                    articleAdapter.submitData(it)
                }
            }
        }
    }


}